package com.farsunset.cim.sdk.server.slc.utils;

import com.farsunset.cim.model.Message;
import com.farsunset.cim.model.Ping;
import com.farsunset.cim.sdk.server.slc.constant.SlcCIMConstant;
import com.farsunset.cim.sdk.server.slc.constant.SlcChannelAttr;
import io.netty.channel.Channel;

/**
 * 渠道写入转换器
 *
 * @author slc
 * @date 30/8/2021 上午 10:27
 */
public class ChannelWriteTransform {
    public static final String KEY_PROTO_BUF = "protobuf";
    public static final String KEY_JSON = "json";
    public static final String KEY_TEXT = "text";
    public static final String KEY_HEX = "hex";

    /**
     * 转换写入对象
     *
     * @param channel
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Object transformWriteObj(Channel channel, T data) {
        return transformWriteObj(channel, data, null);
    }

    /**
     * 转换写入对象
     *
     * @param channel
     * @param data
     * @param interfere
     * @param <T>
     * @return
     */
    public static <T> Object transformWriteObj(Channel channel, T data, Interfere<T> interfere) {
        String writeType = channel.attr(SlcChannelAttr.WRITE_TYPE).get();
        if (interfere != null) {
            Object result = interfere.onWriteType(writeType, data);
            if (result != null) {
                return result;
            }
        }
        if (writeType != null) {
            if (writeType.equals(KEY_PROTO_BUF)) {
                return data;
            } else if (writeType.equals(KEY_JSON)) {
                if (data instanceof Ping) {
                    return SlcCIMConstant.VALUE_PING;
                }
                return toJsonGuarantee(data);
            } else if (writeType.equals(KEY_TEXT) || writeType.equals(KEY_HEX)) {
                if (data instanceof Ping) {
                    return SlcCIMConstant.VALUE_PING;
                } else if (data instanceof String) {
                    if (writeType.equals(KEY_HEX)) {
                        return CimHexUtils.hex2Bytes((String) data);
                    } else {
                        return data;
                    }
                } else if (data instanceof byte[]) {
                    return data;
                } else if (data instanceof Message) {
                    Message message = (Message) data;
                    if (message.getContent() != null && message.getContent().length() > 0) {
                        if (writeType.equals(KEY_HEX)) {
                            return CimHexUtils.hex2Bytes(message.getContent());
                        } else {
                            return message.getContent();
                        }
                    }
                }
                return toJsonGuarantee(data);
            }
        }
        return data;
    }

    private static <T> String toJsonGuarantee(T data) {
        try {
            return CimGsonUtils.toJson(data);
        } catch (Exception e) {
            return "{" + "\"msg\":\"" + "json解析错误" + "\"}";
        }
    }

    /**
     * 转换干扰器
     *
     * @param <T>
     */
    public interface Interfere<T> {
        Object onWriteType(String writeType, T data);
    }
}
